package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardService;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "版块接口")
@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {
    @Value("${forum.index.num:9}")
    private Integer defaultNum;

    @Resource
    private IBoardService boardService;

    @GetMapping("/topList")
    @ApiOperation("获取首页板块列表")
    public AppResult<List<Board>> topList(){
        List<Board> boards=boardService.selectByNum(defaultNum);
        if(boards==null){
            boards=new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @GetMapping("/selectById")
    @ApiOperation("获取版块信息")
    public AppResult<Board> selectById(@ApiParam("版块id") @RequestParam("boardId") @NotNull Long boardId){
        Board board=boardService.selectById(boardId);
        if(board==null||board.getDeleteState()==1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        return AppResult.success(board);
    }
}
